package com.hl.springboot4.service.impl;

import com.hl.springboot4.entity.User;
import com.hl.springboot4.mapper.UserMapper;
import com.hl.springboot4.service.UserService;
import com.hl.springboot4.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @ClassName UserServiceImpl
 * @Description TODO
 * @Author libowen
 * @Date 2025年03月06日 9:18
 * @Version 1.0
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public int login(String username,String password) {
        int code = 0;
        User user = userMapper.findByUsername(username);
        if(user==null){
            code = -1;  // 用户名查不到
        }else if(!user.getPassword().equalsIgnoreCase(MD5Util.encode(username+password))){
            code = -2;  // 密码错误
        }else {
            code = 1;   // 登录成功
        }
        return code;
    }

    @Override
    public User login2(String username, String password) {
        User user = userMapper.findByUsername(username);
        if(user!=null && user.getPassword().equalsIgnoreCase(MD5Util.encode(username+password))){
            return user;
        }
        return null;
    }
}
